package cn.ekuma.epos.qkorder.link;

import javax.swing.JPanel;

import cn.ekuma.epos.datalogic.I_DataLogicSales;

import com.openbravo.bean.Product;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.loader.query.QBFCompareEnum;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.panels.link.AbstractProductSelectTablePanel;
import com.openbravo.pos.panels.model.ProductTableModel;

public class JProductCodeSimilarPanel extends AbstractProductSelectTablePanel<Product> {
    Product product;
	public JProductCodeSimilarPanel(I_DataLogicSales dlSales) {
		super(dlSales, new ProductTableModel());
	}


	@Override
	public void baseBeanChanged(Product o) {
		try {
			product=(Product) dlSales.findByStringId(Product.class, this.baseBeanID);
			if(product.getCode().length()==13){
				tableModel.resert(dlSales.listByFilter(Product.class,createValue1()));
			}else
				tableModel.resert(null);
		} catch (BasicException e) {
			new MessageInf(e).show(this);
            tableModel.resert(null);
		}
	}

	@Override
	public String getProductId(Product obj) {
		return obj.getID();
	}

	@Override
	public JPanel getToolBar() {
		JPanel toolbar=new JPanel();
		buttonGroup1 = new javax.swing.ButtonGroup();
        jCode7 = new javax.swing.JRadioButton();
        jCode8 = new javax.swing.JRadioButton();
        jCode9 = new javax.swing.JRadioButton();
        jCode10 = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jSellPriceEquale = new javax.swing.JCheckBox();

        toolbar.setLayout(new javax.swing.BoxLayout(toolbar, javax.swing.BoxLayout.LINE_AXIS));

        buttonGroup1.add(jCode7);
        jCode7.setText("7"); // NOI18N
        jCode7.setFocusPainted(false);
        jCode7.setFocusable(false);
        toolbar.add(jCode7);

        buttonGroup1.add(jCode8);
        jCode8.setText("8"); // NOI18N
        jCode8.setFocusPainted(false);
        jCode8.setFocusable(false);
        toolbar.add(jCode8);

        buttonGroup1.add(jCode9);
        jCode9.setSelected(true);
        jCode9.setText("9"); // NOI18N
        jCode9.setFocusPainted(false);
        jCode9.setFocusable(false);
        toolbar.add(jCode9);

        buttonGroup1.add(jCode10);
        jCode10.setText("10"); // NOI18N
        jCode10.setFocusPainted(false);
        jCode10.setFocusable(false);
        toolbar.add(jCode10);

        jLabel1.setText("     "); // NOI18N
        toolbar.add(jLabel1);

        jSellPriceEquale.setText(AppLocal.getIntString("label.sellPriceEqual")); // NOI18N
        jSellPriceEquale.setFocusPainted(false);
        jSellPriceEquale.setFocusable(false);
        toolbar.add(jSellPriceEquale);
		return toolbar;
	}

	    private javax.swing.ButtonGroup buttonGroup1;
	    private javax.swing.JRadioButton jCode10;
	    private javax.swing.JRadioButton jCode7;
	    private javax.swing.JRadioButton jCode8;
	    private javax.swing.JRadioButton jCode9;
	    private javax.swing.JLabel jLabel1;
	    private javax.swing.JCheckBox jSellPriceEquale;
	
	    public Object createValue1() throws BasicException {
	        int codeLenger=7;
	        if(jCode8.isSelected())
	        	codeLenger=8;
	        else if(jCode9.isSelected())
	        	codeLenger=9;
	        else if(jCode10.isSelected())
	        	codeLenger=10;
	        if (jSellPriceEquale.isSelected()) {
	            // Filtro por formulario
	            return new Object[] {
	            	QBFCompareEnum.COMP_NONE, null,
		            QBFCompareEnum.COMP_NONE, null,
		            QBFCompareEnum.COMP_EQUALS, product.getPriceSell(),
	                QBFCompareEnum.COMP_NONE, null,
	                QBFCompareEnum.COMP_RE, product.getCode().substring(0, codeLenger),
	                QBFCompareEnum.COMP_DISTINCT, product.getID()
	            };
	        } else {            
	            // Filtro por codigo de barras.
	            return new Object[] {
	                QBFCompareEnum.COMP_NONE, null,
	                QBFCompareEnum.COMP_NONE, null,
	                QBFCompareEnum.COMP_NONE, null,
	                QBFCompareEnum.COMP_NONE, null,
	                QBFCompareEnum.COMP_RE, product.getCode().substring(0, codeLenger),
	                QBFCompareEnum.COMP_DISTINCT, product.getID()
	            };
	        }
	    } 
}
